import http from '@/utils/http'

const BASE_URL = '/education/exam'

export interface ExamResp {
  id: string
  name: string
  examType: string
  classYear: number
  term: string
  grade: string
  subject: string
  totalScore: number
  averageScore: number
  highestScore: number
  lowestScore: number
  studentCount: number
  remark: string
}

export interface ScoreItem {
  questionId: string
  corrected: boolean
  score: number
}

export interface ExamRecordResp {
  examRecordId: string
  examId: string
  studentId: string
  classId: string
  totalScore: string
}

export interface ExamRecordAggregateResp extends ExamRecordResp {
  studentName: string
  className: string
  leadTeacherId: string
  leadTeacherName: string
  answerRecordsMap: Record<string, ScoreItem>
}

export interface QuestionSet {
  key: number
  title: string
  questions: Array<ExamQuestionResp>
}

export interface ExamScoreTableResp {
  id: string
  title: string
  questionSets: Array<QuestionSet>
  examRecords: ExamRecordAggregateResp[]
}

export interface ExamQuestionResp {
  module: any
  id: string
  examId: string
  questionIndex: number
  questionType: string
  score: number
  averageScore: number
  questionModule: string
  knowledgePoints: string
}

export interface ExamDetailResp {
  id: string
  name: string
  examType: string
  classYear: number
  term: string
  grade: string
  subject: string
  totalScore: number
  averageScore: number
  highestScore: number
  lowestScore: number
  studentCount: number
  remark: string
}

export interface ExamQuery extends SortQuery {
  id?: string
  name?: string
  classYear?: number
  term?: string
  grade?: string
  subject?: string
}

export interface ExamPageQuery extends ExamQuery, PageQuery {

}

/** @desc 查询列表 */
export function getList(query: ExamQuery) {
  return http.get<ExamResp[]>(`${BASE_URL}/list`, query)
}

/** @desc 查询详情 */
export function getExamDetail(id: string) {
  return http.get<ExamDetailResp>(`${BASE_URL}/detail/${id}`)
}

export function getExamRecordAggregate(examId: string) {
  return http.get<ExamRecordAggregateResp[]>(`${BASE_URL}/record/list/${examId}`)
}

export function getScoreTable(examId: string) {
  return http.get<ExamScoreTableResp>(`${BASE_URL}/score-table`, { examId })
}

export function getQuestionList(examId: string) {
  return http.get<ExamQuestionResp>(`${BASE_URL}/question-list`, { examId })
}

export function getScoreDist(examId: string) {
  return http.get<ExamScoreTableResp>(`${BASE_URL}/score-dist`, { examId })
}

/** @desc 创建 */
export function create(data: any) {
  return http.post(`${BASE_URL}/create`, data)
}

/** @desc 修改 */
export function update(data: any, id: string) {
  return http.post(`${BASE_URL}/update/${id}`, data)
}

/** @desc 删除 */
export function remove(id: string) {
  return http.post(`${BASE_URL}/delete/${id}`)
}

/** 文件上传 */
export function upload(formData: any) {
  return http.post(`${BASE_URL}/import`, formData)
}
